import { getStorage, removeStore, setStorage } from "@/utils/index"
import { defineStore } from "pinia"

interface Props {
  token:string,
  userInfo?: Record<string, string>
}

const userStore = defineStore("user",{
  state():Props {
    return {
      token: getStorage("token") || "",
      userInfo: getStorage("userInfo") || undefined
    }
  },
  getters:{

  },
  actions:{
    async setToken(data) {
      this.token = data;
      await setStorage("token", data);
    },
    async setUserInfo(data) {
      this.userInfo = data;
      await setStorage("userInfo", data);
    },
    async clearUserInfo(){
      this.token = "";
      this.userInfo = undefined;
      await removeStore("token")
      await removeStore("userInfo")
    }
  }
})

export default userStore
